package com.example.testproj;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Main3Activity extends AppCompatActivity {
    private static final String TAG = "Main3Activity";
    private Handler handler = new Handler();
    private ImageView imageView;
    private RecyclerView recyclerView;
    private int pressPos = 0;
    private List<Key> keyList = new ArrayList<>();
    private CpuTest cpuTest;
    private Thread playThread;
    private String[] gameList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        imageView = (ImageView) findViewById(R.id.imageview);
        cpuTest = new CpuTest(this, handler, imageView);


        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(this, 4);
        recyclerView.setLayoutManager(layoutManager);

        initKeys();

        recyclerView.setAdapter(new MyAdapater());
        try {
            gameList = getResources().getAssets().list("roms");
        } catch (IOException e) {
            e.printStackTrace();
        }

        findViewById(R.id.selGame).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showSelDialog();
            }
        });
        playGame("IBM");
    }

    private void playGame(final String name) {
        if (playThread != null) {
            playThread.interrupt();
        }
        playThread = new Thread() {
            @Override
            public void run() {
                try {
                    cpuTest.run(name);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        };

        playThread.start();
    }

    private void showSelDialog() {

            AlertDialog.Builder builder = new AlertDialog.Builder(this);

            ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, gameList);


            ListView listView = new ListView(this);
            listView.setAdapter(arrayAdapter);
            builder.setView(listView);
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });
            final Dialog dialog = builder.create();
            dialog.show();
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    dialog.dismiss();
                    playGame(gameList[position]);
                }
            });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        playThread.interrupt();
        cpuTest.onDestroy();
    }

    private void initKeys() {
        keyList.add(new Key("1", 0));
        keyList.add(new Key("2", 1));
        keyList.add(new Key("3", 2));
        keyList.add(new Key("4", 3));
        keyList.add(new Key("Q", 4));
        keyList.add(new Key("W", 5));
        keyList.add(new Key("E", 6));
        keyList.add(new Key("R", 7));
        keyList.add(new Key("A", 8));
        keyList.add(new Key("S", 9));
        keyList.add(new Key("D", 10));
        keyList.add(new Key("F", 11));
        keyList.add(new Key("Z", 12));
        keyList.add(new Key("X", 13));
        keyList.add(new Key("C", 14));
        keyList.add(new Key("V", 15));
    }

    class MyAdapater extends RecyclerView.Adapter<MyAdapater.MyViewHolder> {

        @Override
        public MyAdapater.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(Main3Activity.this).inflate(R.layout.key_layout, parent, false));
        }

        @Override
        public void onBindViewHolder(MyAdapater.MyViewHolder holder, int position) {
            holder.itemView.setTag(keyList.get(position).value);
            holder.textView.setText(keyList.get(position).name);
        }

        @Override
        public int getItemCount() {
            return keyList.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder implements View.OnTouchListener {
            private Button textView;

            public MyViewHolder(View itemView) {
                super(itemView);

                textView = (Button) itemView.findViewById(R.id.imageview);
                textView.setClickable(false);
                itemView.setOnTouchListener(this);
            }

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int value = (int) v.getTag();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN://触摸屏幕时，只执行一次
                        cpuTest.pressKey(value);
                        Log.i(TAG, "执行触摸时的操作");
                        break;
                    case MotionEvent.ACTION_MOVE://触摸到屏幕时，反复执行
                        Log.i(TAG, "执行移动时的操作");
                        break;
                    case MotionEvent.ACTION_UP://离开屏幕时，只执行一次
                        Log.i(TAG, "执行触摸离开时的操作");
                        cpuTest.unPressKey(value);
                        break;
                }
                return true;
            }
        }
    }

    class Key {
        private String name;
        private int value;

        public Key(String name, int value) {
            this.name = name;
            this.value = value;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }
    }

}
